﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Tcvt.Packages.BuddyBuildServiceProxy;
using System.Xml;
using System.Xml.Linq;
using System.Globalization;

namespace Tcvt.Packages
{
    class MsBuildPropertyConverter : TypeConverter 
    {
        // Overrides the CanConvertFrom method of TypeConverter.
        // The ITypeDescriptorContext interface provides the context for the
        // conversion. Typically, this interface is used at design time to 
        // provide information about the design-time container.
        public override bool CanConvertFrom(ITypeDescriptorContext context,
           Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }

        // Overrides the ConvertFrom method of TypeConverter.
        public override object ConvertFrom(ITypeDescriptorContext context,
           CultureInfo culture, object value)
        {
            if (value is string)
            {
                string msBuildPropertyString = (string)value;
                XElement msBuildPropertyXml = XElement.Load(new XmlTextReader(msBuildPropertyString, XmlNodeType.Element, null));

                if (msBuildPropertyXml != null)
                {
                    int numberOfElements = msBuildPropertyXml.Elements("MsBuildProperty").Count<XElement>();

                        MsBuildProperty[] msBuildPropertyArray = new MsBuildProperty[numberOfElements];

                        if (numberOfElements > 0)
                        {

                            int counter = 0;
                            foreach (XElement element in msBuildPropertyXml.Elements("MsBuildProperty"))
                            {
                                MsBuildProperty msBuildProperty = new MsBuildProperty()
                                {
                                    Name = element.Element("Name").Value,
                                    DefaultValue = element.Element("DefaultValue").Value,
                                    Description = element.Element("Description").Value,
                                    PropertyDataType = element.Element("PropertyDataType").Value,
                                    ValidationExpression = element.Element("ValidationExpression").Value
                                };
                                msBuildPropertyArray[counter] = msBuildProperty;
                                counter++;
                            }
                        }

                        return msBuildPropertyArray;
                }
            }
            return base.ConvertFrom(context, culture, value);
        }

        // Overrides the ConvertTo method of TypeConverter.
        public override object ConvertTo(ITypeDescriptorContext context,
           CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                MsBuildProperty[] msBuildProperty = (MsBuildProperty[])value;
                // Convert to XML fragment:
                XElement msBuildPropertyXml = new XElement("MsBuildPropertyArray");

                if (msBuildProperty != null && msBuildProperty.Length > 0)
                {
                    for (int i = 0; i < msBuildProperty.Length; i++)
                    {
                        XElement msBuildPropertyXmlFragment = new XElement("MsBuildProperty",
                            new XElement("Name", msBuildProperty[i].Name),
                            new XElement("DefaultValue", msBuildProperty[i].DefaultValue),
                            new XElement("Description", msBuildProperty[i].Description),
                            new XElement("PropertyDataType", msBuildProperty[i].PropertyDataType),
                            new XElement("ValidationExpression", msBuildProperty[i].ValidationExpression)
                        );
                        msBuildPropertyXml.Add(msBuildPropertyXmlFragment);
                    }
                }

                return msBuildPropertyXml.ToString();
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}
